<?php
	require_once('../utils/query.php');
  	require_once('../utils/logged_in.php');

	$num = $_POST["da"];
	$info = explode("~", $num); // Breaking the string apart containing first the data2.length then the facebookuserid and then the name separated by a ~
	$num = $info[0]; //  $num now contains the length of the data set passed in of facebook userids and names
	$cnt = 1;
	$i = 0;
	$uids = array();
	$names = array();
	for($i=0;$i<$num;$i++) // fill the $uids array with the facebookuids and $names array with the facebook names
	{
		$uids[] = $info[$cnt];
		$names[] = $info[$cnt+1];
		$cnt = $cnt + 2; 
	}

	
?>

<!--<ul class="trades">-->
<ul>
	<?php
		$i = 0;
		for($i=0;$i<count($uids);$i++)
		{
			$result = execute_query("SELECT drupaluid FROM facebookUser WHERE facebookID='%s'", $uids[$i]); // Match the facebook id's with the drupal uids, so $result is an array of drupaluids
			if($row = mysql_fetch_array($result)) // if $result contained some data. $row refers to friends
			{
				$use = execute_query("SELECT tradeid FROM Trade WHERE (userid1='%s' AND userid2='%s') OR (userid1='%s' AND userid2='%s')", $drupaluid, $row[0], $row[0], $drupaluid);
				if($use = mysql_fetch_array($use))
				{
					$use = execute_query("SELECT * FROM TradePossession WHERE tradeid='%s'", $use[0]); // $use is currently an array of trade id's
						if($r = mysql_fetch_array($use)) // If there is a possession that matches with the tradeID in the table e.g. checking if there is a trade
						{
							echo "<a href='javascript:loadCardsPlus(\"\", $uids[$i])'><b> "; // Bold the name of the person that matches the userID
							echo "Trade with: ";
							echo $names[$i];
							echo "</b></a>";
							echo "<br />";
							echo "<br />";
						}
						else
						{
							//echo "<a href='javascript:loadCardsPlus(\"\", $uids[$i])'> ";
							// test:
							echo "<a href='javascript:tradeWithUser(\"\", $uids[$i])'> ";
							// end test
							echo "Trade with: ";
							echo $names[$i];
							echo "</a>";
							echo "<br />";
							echo "<br />";
						}
				}
				else
				{
					//echo "<a href='javascript:loadCardsPlus(\"\", $uids[$i])'> ";
					// test:
					echo "<a href='javascript:tradeWithUser(\"\", $uids[$i])'> ";
					// end test
					echo "Trade with: ";
					echo $names[$i];
					echo "</a>";
					echo "<br />";
					echo "<br />";
				}
			}
		}



	?>


	
</ul>
